<?php
/**
 * Template Name: Web Service
 */
/**
 * Created by JetBrains PhpStorm.
 * User: Nathan.Nelson
 * Date: 02/12/12
 * Time: 13:11
 * To change this template use File | Settings | File Templates.
 */
$json = array();

$action = $_POST['action'];
$json['action'] = $action;

switch ($action) {
    case 'user_exists':
        $json['performed'] = $action;
        $user_email = $_POST['form_email'];
        $info = (username_exists($user_email) || email_exists($user_email));
        break;
    case 'register':

        break;
    case 'book':

        break;
    case 'get_date_slots':

        break;
    case 'email_team':
        $json['performed'] = $action;

        $form = $_REQUEST['form'];

        $json['data'] = emailTeam (
            $form['form_team'],
            $form['form_booking'],
            $form['form_comments'],
            $form['form_user']
        );

        $json['received'] = $form;
        //$json['message'] = 'Deleted Team '.$_REQUEST['data']['team'];
        $json['status'] = 'success';
        break;
    case 'email_booking':
        $json['performed'] = $action;

        $form = $_REQUEST['form'];

        $json['data'] = emailBooking (
            $form['form_booking'],
            $form['form_comments'],
            $form['form_user']
        );

        $json['received'] = $form;
        //$json['message'] = 'Deleted Team '.$_REQUEST['data']['team'];
        $json['status'] = 'success';
        break;
    case 'add_team':
        $json['performed'] = $action;
        $form = $_REQUEST['form'];

        $data = array(
            "colour" => $form['team_colour'],
            "name" => $form['team_name'],
            "players" => $form['team_players']
        );

        $json['response'] = newTeam($data);

        $json['url'] = get_permalink($json['response']['post']);
        $json['status'] = 'success';
        break;
    case 'save_user':
        $json['performed'] = $action;
        $form = $_REQUEST['form'];

        update_field('first_name', $form['first_name'], 'user_'.$form['user']);
        update_field('last_name', $form['last_name'], 'user_'.$form['user']);
        update_field('phone', $form['phone'], 'user_'.$form['user']);
        wp_update_user( array ( 'ID' => $form['user'], 'user_email' => $form['email'] ) ) ;

        $json['status'] = 'success';
        break;
    case 'save_venue':
        $json['performed'] = $action;
        $form = $_REQUEST['form'];

        update_field('name', $form['name'], $form['venue']);
        update_field('address', $form['address'], $form['venue']);
        update_field('website', $form['website'], $form['venue']);
        update_field('opening', $form['opening'], $form['venue']);
        update_field('primary_contact_email', $form['primary_contact_email'], $form['venue']);
        update_field('primary_contact_number', $form['primary_contact_number'], $form['venue']);
        update_field('primary_contact_name', $form['primary_contact_name'], $form['venue']);
        update_field('other_contact_details', $form['other_contact_details'], $form['venue']);
        update_field('nearest_station', $form['nearest_station'], $form['venue']);
        update_field('changing_rooms', $form['changing_rooms'], $form['venue']);
        update_field('showers', $form['showers'], $form['venue']);
        update_field('bar', $form['bar'], $form['venue']);
        update_field('parking', $form['parking'], $form['venue']);
        wp_update_user( array ( 'ID' => $form['user'], 'user_email' => $form['email'] ) ) ;

        $json['status'] = 'success';
        break;
    case 'save_team':
        $json['performed'] = $action;
        $form = $_REQUEST['form'];

        $data = array(
            "colour" => $form['team_colour'],
            "name" => $form['team_name'],
            "players" => $form['team_players']
        );

        update_field('name', $form['team_name'], $form['team']);
        update_field('colour', $form['team_colour'], $form['team']);
        update_field('players', $form['team_players'], $form['team']);

        $json['status'] = 'success';
        break;
    case 'set_team1':
        $json['performed'] = $action;

        update_field('team1', $_REQUEST['data']['team'], $_REQUEST['data']['booking']);

        $json['message'] = 'Added Team 1 - '.$_REQUEST['data']['team'].' to booking '.$_REQUEST['data']['booking'];
        $json['status'] = 'success';
        break;
    case 'remove_team':
        $json['performed'] = $action;

        wp_delete_post($_POST['data']['team']);

        $json['message'] = 'Deleted Team '.$_REQUEST['data']['team'];
        $json['status'] = 'success';
        break;
    case 'set_team2':
        $json['performed'] = $action;

        update_field('team2', $_REQUEST['data']['team'], $_REQUEST['data']['booking']);

        $json['message'] = 'Added Team 2 - '.$_REQUEST['data']['team'].' to booking '.$_REQUEST['data']['booking'];
        $json['status'] = 'success';
        break;
    case 'activate_slot':
        $json['performed'] = $action;
        update_field('active', true, $_POST['the_slot']);

        $json['status'] = 'success';
        break;
    case 'deactivate_slot':
        $json['performed'] = $action;
        update_field('active', false, $_POST['the_slot']);

        $json['status'] = 'success';
        break;
    case 'remove_slot':
        $json['performed'] = $action;
        wp_delete_post($_POST['the_slot']);

        $json['status'] = 'success';
        break;
    case 'cancel_booking':
        $json['performed'] = $action;
        wp_delete_post($_POST['the_booking']);

        $json['status'] = 'success';
        break;
    case 'remove_special':
        $json['performed'] = $action;
        wp_delete_post($_POST['the_special']);

        $json['status'] = 'success';
        break;
    case 'internal_booking':
        $json['performed'] = $action;
        $form = $_POST['form'];


        $data = array(
            "slot" => $form['form_slot'],
            "date" => date('Ymd', strtotime($form['form_date'])),
            "customer_id" => $form['form_user'],
            "email_list" => '',
            "comments" => $form['form_comments'],
            "authorised" => true,
            "internal" => true
        );

        $json['response'] = newBooking($data);

        $json['status'] = 'success';
        break;
    case 'add_special':
        $json['performed'] = $action;
        $form = $_POST['form'];

        $data = array(
            "slot" => $form['form_slot'],
            "date" => date('Ymd', strtotime($form['form_date'])),
            "price" => $form['form_special']
        );

        $json['response'] = newSpecial($data);

        $json['status'] = 'success';
        break;
    case 'get_venue_week':
        $json['performed'] = $action;
        $json['slots'] = array();
        $json['bookings'] = array();
        $json['specials'] = array();

        $q = new WP_Query(array('post_type' => 'slot', 'order' => 'ASC', 'posts_per_page' => 100000));
        while ($q->have_posts()) : $q->the_post();
            if (getVenueID($q->post->ID) == $_POST['the_venue']) {
                $obj = get_fields($q->post->ID);
                $obj['id'] = $q->post->ID;
                $obj['name'] = get_field('pid', getPitchID($q->post->ID));
                $obj['link'] = get_permalink($q->post->ID);
                $obj['pitch'] = get_permalink(getPitchID($q->post->ID));
                $obj['price'] = get_field('price', $q->post->ID);
                array_push($json['slots'], $obj);
            }
        endwhile;

        $q = new WP_Query(array('post_type' => 'booking', 'order' => 'ASC', 'posts_per_page' => 100000));
        while ($q->have_posts()) : $q->the_post();
            if (getVenueID($q->post->ID) == $_POST['the_venue']) {
                $obj = get_fields($q->post->ID);
                $obj['id'] = $q->post->ID;
                $obj['link'] = get_permalink($q->post->ID);
                array_push($json['bookings'], $obj);
            }
        endwhile;

        $q = new WP_Query(array('post_type' => 'special', 'order' => 'ASC', 'posts_per_page' => 100000));
        while ($q->have_posts()) : $q->the_post();
            if (getVenueID($q->post->ID) == $_POST['the_venue']) {
                $obj = get_fields($q->post->ID);
                $obj['id'] = $q->post->ID;
                array_push($json['specials'], $obj);
            }
        endwhile;

        break;
    case 'add_slot':
        $json['performed'] = $action;
        $json['newSlots'] = array();
        $json['deniedSlots'] = array();
        $json['status'] = '';

        $form = $_POST['form'];

        if ($form['form_active'] == 'true') {
            $active = true;
        }


        $data = array(
            "pitch" => $form['form_pitch'],
            "start_time" => $form['form_start_time'],
            "end_time" => $form['form_end_time'],
            "day" => $form['form_day'],
            "price" => $form['form_price'],
            "active" => $active
        );

        if ($form['intervals'] == "") {
            $json = slotQuery($data, $form,$json);
        } else {
            $start = strtotime($form['form_start_time']);
            $end = strtotime($form['form_start_time'] . ' +' . $form['intervals'] . ' minutes');
            $stop = strtotime($form['form_end_time']);
            $count = 0;
            while ($end < $stop) {
                $start = strtotime($form['form_start_time'] . ' +' . ($form['intervals'] * $count) . ' minutes');
                $end = strtotime($form['form_start_time'] . ' +' . ($form['intervals'] * ($count + 1)) . ' minutes');
                $data['start_time'] = intval(date('Hi',$start));
                $data['end_time'] = intval(date('Hi',$end));
                if($end > $stop) {break;}
                $json = slotQuery($data, $form,$json);
                $count++;
            }
        }


        break;
    default:
        $json['performed'] = "No Action Performed";
}
$json['info'] = $info;
echo json_encode($json);